//
//  AmendViewController.m
//  cm
//
//  Created by suyibaishan on 2017/11/4.
//  Copyright © 2017年 feihu. All rights reserved.
//

#import "AmendViewController.h"
#import "AmendCell.h"

@interface AmendViewController () <UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
{
    UITableView *_tableView;
    UIView *headView;
    NSArray *titleArr;
    UILabel *menberLabel;
    NSString *address;
}
@end

@implementation AmendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createAmendView];
}

-(void)createAmendView
{
    self.titleLabel.text = @"个人信息修改";
    self.backButton.hidden = NO;
    
    titleArr = @[@"用户账号",@"真实姓名",@"身份证号码",@"家庭住址"];
    
    [self createHeadView];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.baseBarHeight, kScreenWidth, kScreenHeight - self.baseBarHeight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    _tableView.tableHeaderView = headView;
    
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 80)];
    _tableView.tableFooterView = footView;
    
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    saveBtn.layer.cornerRadius = 20;
    saveBtn.backgroundColor = [UIColor colorWithRed:0.19 green:0.53 blue:0.98 alpha:1];
    [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [saveBtn setTitle:@"保存个人信息" forState:UIControlStateNormal];
    saveBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [saveBtn addTarget:self action:@selector(saveBtnHandler) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:saveBtn];
    saveBtn.sd_layout
    .leftSpaceToView(footView, 30)
    .topSpaceToView(footView, 20)
    .widthIs(kScreenWidth - 60)
    .heightIs(40);
}

-(void)createHeadView
{
    headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 90)];
    
    menberLabel = [[UILabel alloc] init];
    menberLabel.layer.cornerRadius = 12.5;
    menberLabel.clipsToBounds = YES;
    menberLabel.textAlignment = NSTextAlignmentCenter;
    menberLabel.font = [UIFont systemFontOfSize:14];
    menberLabel.text = [NSString stringWithFormat:@"• %@    ",self.dataSource[@"realName"]];
    menberLabel.textColor = [UIColor whiteColor];
    menberLabel.backgroundColor = [UIColor colorWithRed:0.19 green:0.53 blue:0.98 alpha:1];
    [headView addSubview:menberLabel];
    menberLabel.sd_layout
    .topSpaceToView(headView, 35)
    .leftSpaceToView(headView, 20)
    .widthIs(60)
    .heightIs(25);
    [menberLabel setSingleLineAutoResizeWithMaxWidth:115];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    [headView addSubview:line];
    line.sd_layout
    .leftSpaceToView(menberLabel, 0)
    .topSpaceToView(headView, 47.5)
    .widthIs(kScreenWidth - 115)
    .heightIs(0.5);
    
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.textAlignment = NSTextAlignmentRight;
    timeLabel.font = [UIFont systemFontOfSize:12];
    timeLabel.text = [NSString stringWithFormat:@"加入时间:%@",self.dataSource[@"createTime"]];
    timeLabel.textColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1];
    [headView addSubview:timeLabel];
    timeLabel.sd_layout
    .topSpaceToView(line, 0)
    .rightSpaceToView(headView, 30)
    .widthIs(kScreenWidth/2+20)
    .heightIs(15);
}

-(void)saveBtnHandler
{
    [self.view endEditing:YES];
    [[ToolNet sharedManager] changeInfo:[mUserDefaults objectForKey:@"userCode"] accessToken:[mUserDefaults objectForKey:@"accessToken"] address:address url:MChangeInfo completionHandler:^(NSURLSessionDataTask *task, NSDictionary *responseJson, NSError *error) {
        if ([responseJson[@"code"] isEqual:@1]) {
            [self.navigationController popViewControllerAnimated:YES];
            [TSMessage showMessage:@"修改成功" type:TSMessageNotificationTypeSuccess];
        }
        else {
            [self showMessage:responseJson[@"msg"]];
        }
    }];
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return titleArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cellIdentifier";
    AmendCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[AmendCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLabel.text = titleArr[indexPath.row];
    cell.contentField.delegate = self;
    
    switch (indexPath.row) {
        case 0:
            cell.contentField.hidden = YES;
            cell.detailLabel.hidden = NO;
            cell.detailLabel.text = self.dataSource[@"mobile"];
            break;
        case 1:
            cell.contentField.hidden = YES;
            cell.detailLabel.hidden = NO;
            cell.detailLabel.text = self.dataSource[@"realName"];
            break;
        case 2:
            cell.contentField.hidden = YES;
            cell.detailLabel.hidden = NO;
            cell.detailLabel.text = self.dataSource[@"card"];
            break;
        case 3:
            cell.contentField.hidden = NO;
            cell.detailLabel.hidden = YES;
            cell.detailLabel.text = self.dataSource[@"address"];
            break;
        default:
            break;
    }

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

#pragma - UITextFieldDelegate
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    AmendCell *cell = (AmendCell *)[[[textField superview] superview] superview];
    NSIndexPath *indexPath = [_tableView indexPathForCell:cell];
    if (indexPath.row == 3){
        address = textField.text;
    }
    [textField resignFirstResponder];
}

#pragma mark - UITextField Delegate methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    AmendCell *cell = (AmendCell *)[[[textField superview] superview] superview];
    NSIndexPath *indexPath = [_tableView indexPathForCell:cell];
    if (indexPath.row == 3){
        address = textField.text;
    }
    [textField resignFirstResponder];
    
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
